<?php

class CompareController extends GxController {

    public function filters() {
        return array(
            'accessControl', // perform access control for CRUD operations
        );
    }

    public function accessRules() {
        return array(
            array('allow', // allow all users to perform 'index' and 'view' actions
                'actions' => array('CompareclassAddClassById', 'RemovecompareclasscourseById', 'view'),
                'users' => array('*'),
            ),
            array('allow', // allow authenticated user to perform 'create' and 'update' actions
                'actions' => array(''),
                'users' => array('@'),
            ),
            array('deny', // deny all users
                'users' => array('*'),
            ),
        );
    }

    public function actionCompareclassAddClassById() {
        $this->layout = FALSE;
        $checkIds = '';
        if (Yii::app()->session['allcompare_ids'] <> "") {
            $compare_class_course_id = Yii::app()->session['allcompare_ids'];
            $checkIds = rtrim($compare_class_course_id, ',');
            $checkIds = explode(',', $checkIds);
            if (in_array($_POST['class_course_id'], $checkIds) || count($checkIds) >= 4) {
                return false;
            }
        } else {
            $compare_class_course_id = '';
            Yii::app()->session['classcoursetype'] = '';
        }



        if ($_POST['classcoursetype'] == 'class') {
            $compare_class_course_id.= $_POST['class_course_id'] . "@" . $_POST['classcoursetype'] . ',';
            Yii::app()->session['allcompare_ids'] = $compare_class_course_id;
            $classcourseDetail = Classes::model()->findByPk($_POST['class_course_id']);
            $class_course_titile = $classcourseDetail->class_titile;
            $imagePath = getImagePathByClassCourse('Classes', $_POST['class_course_id']);
            $imageName = (ClassImage::getPrimaryImage($_POST['class_course_id']) != '') ? getRequestbaseUrl() . getuploadFolderDir() . SkillConfig::getClassCourseImagePath($imagePath) . 'thumbs/' . ClassImage::getPrimaryImage($_POST['class_course_id']) : getWorkNoshopimage(SkillConfig::skill_myinbox_image);
        } else {
            $compare_class_course_id.= $_POST['class_course_id'] . "@" . $_POST['classcoursetype'] . ',';
            Yii::app()->session['allcompare_ids'] = $compare_class_course_id;
            $classcourseDetail = Courses::model()->findByPk($_POST['class_course_id']);
            $class_course_titile = $classcourseDetail->course_titile;
            $imagePath = getImagePathByClassCourse('Courses', $_POST['class_course_id']);
            $imageName = (ClassImage::getPrimaryImage($_POST['class_course_id']) != '') ? getRequestbaseUrl() . getuploadFolderDir() . SkillConfig::getClassCourseImagePath($imagePath) . 'thumbs/' . ClassImage::getPrimaryImage($_POST['class_course_id']) :getWorkNoshopimage(SkillConfig::skill_myinbox_image);
        }

        $classcourseImg = '<img src="' . $imageName . '" height="70" width="70"  alt="' . $class_course_titile . '">';

        if (Yii::app()->session['allcompare_ids'] <> "") {
            $checkIds = rtrim($compare_class_course_id, ',');
            $checkIds = explode(',', $checkIds);
        }

        echo $return_data = substr($class_course_titile, 0,20) . '#@#' . $classcourseImg . '#@#' . count($checkIds);
    }

    public function actionRemovecompareclasscourseById() {
        $this->layout = FALSE;
        $OutPutdata = '';
        $remainingIds = '';
        if (Yii::app()->session['allcompare_ids'] <> "") {
            $compare_class_course_id = Yii::app()->session['allcompare_ids'];
            $checkIds = rtrim($compare_class_course_id, ',');
            $checkIds = explode(',', $checkIds);
            $key = array_search($_POST['class_course_id'], $checkIds);
            if (false !== $key) {
                unset($checkIds[$key]);
            }
        }
        if (count($checkIds) > 0) {
            for ($i = 0; $i <= count($checkIds); $i++) {
                if (isset($checkIds[$i]) && $checkIds[$i] <> "") {
                    $remainingIds.=$checkIds[$i] . ',';
                    $getcheckIds = explode("@", $checkIds[$i]);
                    if (isset($getcheckIds[0]) && $getcheckIds[1] == "class") {
                        /* This is for class */
                        $classcourseDetail = Classes::model()->findByPk($getcheckIds[0]);
                        $class_course_titile = $classcourseDetail->class_titile;
                        $imagePath = getImagePathByClassCourse('Classes', $getcheckIds[0]);
                        $imageName = (ClassImage::getPrimaryImage($getcheckIds[0]) != '') ? getRequestbaseUrl() . getuploadFolderDir() . SkillConfig::getClassCourseImagePath($imagePath) . 'thumbs/' . ClassImage::getPrimaryImage($getcheckIds[0]) : getWorkNoshopimage(SkillConfig::skill_myinbox_image);
                    } elseif (isset($getcheckIds[0]) && $getcheckIds[1] == "course") {
                        /* This is for course */
                        $classcourseDetail = Courses::model()->findByPk($getcheckIds[0]);
                        $class_course_titile = $classcourseDetail->course_titile;
                        $imagePath = getImagePathByClassCourse('Courses', $getcheckIds[0]);
                        $imageName = (ClassImage::getPrimaryImage($getcheckIds[0]) != '') ? getRequestbaseUrl() . getuploadFolderDir() . SkillConfig::getClassCourseImagePath($imagePath) . 'thumbs/' . ClassImage::getPrimaryImage($getcheckIds[0]) : getWorkNoshopimage(SkillConfig::skill_myinbox_image);
                    }
                    $classcourseImg = '<img src="' . $imageName . '" height="70" width="70"  alt="' . $class_course_titile . '">';

                    $OutPutdata.='<div class="compare-division" id="' . $getcheckIds[0] . '"><div class="thumbnail">' . $classcourseImg . '<span>' . substr($class_course_titile, 0,20) . '</span>
                        </div><a href="javascript:void(0)" onclick="RemoveCompareList(' . $classcourseDetail->id . ',\'' . $getcheckIds[1] . '\');" class="xlose-button"></a><input type="hidden" class="compareids" name="class_course_id[]" value="' . $getcheckIds[0] . '" /></div> ';
                }
            }
        } else {
            $session = new CHttpSession;
            unset($_SESSION['allcompare_ids']);
            unset($_SESSION['classcoursetype']);
            $OutPutdata = SkillConfig::STATUS_DEACTIVE;
        }
        Yii::app()->session['allcompare_ids'] = $remainingIds;

        echo $OutPutdata;
    }

    public function actionView() {
        if (!empty($_POST['class_course_id'])) {
            $session = new CHttpSession;
            unset($_SESSION['allcompare_ids']);
            unset($_SESSION['classcoursetype']);
            $_POST['class_course_id'] = $_POST['class_course_id'];
            $this->render('view', array('allcompareClasses' => $_POST['class_course_id']));
        } else {
            throw new CHttpException(400, Yii::t('app', Message::Invalid_Request));
        }
    }

}
